/**
	Shiokaze desktop
	Copyright (c) 2011 Shiokaze development team

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <MainWindow.hh>

#include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>
#include <QtGui/QPushButton>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QPixmap>
#include <QtCore/QSettings>

#include <IconWidget.hh>

MainWindow::MainWindow(QWidget *parent)
	: QWidget(parent) {
	// Load settings
	QString path = getenv("HOME");
	path += "/.shiokaze/desktop.conf";
	QSettings settings(path, QSettings::NativeFormat);

	// Set window flags
	setWindowFlags(Qt::FramelessWindowHint);// | Qt::WindowStaysOnBottomHint);
	//resize(QApplication::desktop()->size());

	createWidgets();

	// Load default image
	QString img = settings.value("LookAndFeel/Wallpaper",
								 "/usr/share/shiokaze/data/default.jpg").toString();
	QPixmap pix(img);
	_wallpaper->setPixmap(pix);
	_wallpaper->setScaledContents(true);
}

MainWindow::~MainWindow() {
}

void MainWindow::setWallpaperOpt(const QString &image) {
	// Load settings
	QString path = getenv("HOME");
	path += "/.shiokaze/desktop.conf";
	QSettings settings(path, QSettings::NativeFormat);

	// Set new wallpaper
	wallpaperOpt = image;
	QPixmap pix(image);
	_wallpaper->setPixmap(pix);
	_wallpaper->setScaledContents(true);
	// Save option
	settings.setValue("LookAndFeel/Wallpaper", image);
	settings.sync();
}

void MainWindow::createWidgets() {
	_wallpaper = new QLabel(this);
	_wallpaper->setGeometry(geometry());

	QGridLayout* layout = new QGridLayout(this);
	layout->setSpacing(2);
	{
		const int iconSize = 60;
		const int maxIcons = geometry().width() / iconSize;
		for (int i = 0; i < 10; ++i) {
			for (int j = 0; j < maxIcons; ++j) {
				IconWidget* icon = new IconWidget("/usr/share/shiokaze/data/menu.png",
												  "Close", this);
				icon->setFixedSize(60, 60);
				connect(icon, SIGNAL(clicked()), this, SLOT(close()));
				layout->addWidget(icon, i, j);
			}
		}
	}
	setLayout(layout);
}
